package com.wnhz.work.yz.fragment.group;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.GoToAddPersonActivity;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.AddPersonOneBean;
import com.wnhz.work.yz.bean.FinishGroup2Bean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2017/4/7.
 */
public class AddPersonToPersonFragment extends Fragment {
    private View view;
    private GoToAddPersonActivity activity;
    private RecyclerView recycler;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        view = inflater.inflate(R.layout.fragment_add_person, container, false);
        activity = (GoToAddPersonActivity) getActivity();
        initView();

        return view;
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }

    // 在Android的主线程中运行
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getFinish(FinishGroup2Bean b) {
        Log.e("13123", "dasdsad");
        Intent intent = new Intent();
        intent.putExtra("type", 1);
        ArrayList<AddPersonOneBean.InfoEntity> info = new ArrayList<>();
        for (int i = 0; i < this.infos.size(); i++) {
            boolean flag = false;

            if (infos.get(i).isSelect()) {
                for (int k = 0; k < info.size(); k++) {
                    if (info.get(k).getUser_id().equals(infos.get(i).getUser_id())) {
                        flag = true;
                    }
                }
                if (!flag) {
                    info.add(infos.get(i));
                }
            }
        }
//        if (b.getType() == 1) {
//            if (info.size() > 1) {
//                Toast.makeText(activity, "责任人只能有一个", Toast.LENGTH_SHORT).show();
//                return;
//            }
//        }
        intent.putExtra("user", info);
        activity.setResult(Activity.RESULT_OK, intent);
        activity.finish();

    }

    @Override
    public void onResume() {
        upLoad();
        super.onResume();
    }

    private List<AddPersonOneBean.InfoEntity> infos = new ArrayList<>();

    private void upLoad() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("isTeam", 2);
        activity.showDialog();

      /*  Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----" + key, ":" + value + "\n");
            }
        }*/
        XUtil.Post(Url.GETTEAMLISTBYISTEAM, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        AddPersonOneBean bean = gson.fromJson(result, AddPersonOneBean.class);
                        infos = bean.getInfo();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        activity.MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                activity.closeDialog();
                setData();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                activity.closeDialog();
            }
        });
    }

    private void setData() {
        recycler.setAdapter(new BaseRVAdapter(activity, infos) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_person;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                x.image().bind(holder.getImageView(R.id.head_img), infos.get(position).getUser_img());
                holder.setTextView(R.id.name, infos.get(position).getUser_name());
                holder.setTextView(R.id.phone, infos.get(position).getTelephone());
                ImageView ischoose = holder.getImageView(R.id.ischoose);
                if (infos.get(position).isSelect()) {
                    ischoose.setImageResource(R.mipmap.ic_choose_li);
                } else {
                    ischoose.setImageResource(R.mipmap.ic_choose_nor);
                }
                ischoose.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        infos.get(position).setSelect(!infos.get(position).isSelect());
                        notifyDataSetChanged();
                    }
                });
            }
        });
    }

    private void initView() {
        recycler = (RecyclerView) view.findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
    }
}
